package network;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    Socket socket = null;
    String ip = "";
    int port = 0;
    TcpEchoClient(String serverIp, int port) throws IOException {
        socket = new Socket(serverIp, port);
    }
    public void start() {
        Scanner scanner = new Scanner(System.in);
        System.out.printf("客户端启动\n");
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()) {
            while (true){
                PrintWriter printWriter = new PrintWriter(outputStream);
                Scanner scannerNetWork = new Scanner(inputStream);
                System.out.printf("-> ");
                String send = scanner.next();
                printWriter.println(send);
                printWriter.flush();
                String response = scannerNetWork.next();
                System.out.println(response);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient client = new TcpEchoClient("127.0.0.1", 1314);
        client.start();
    }
}
